
import java.util.*;

public class Game {

    public static void main(String[] args) {
        int categorys = 10;
        int dispelGroupNum = 9;
        List<FullList<Integer>> cardMap = initCardMap();
        int[] randomCards = generatorRandomCards(dispelGroupNum, categorys);
        System.out.println(Arrays.toString(randomCards));
        mappingRandomCardsToCardMap(cardMap, randomCards);
        List<Node> coverageRelations = generateConverageRelations(cardMap);
        List<Node> viewCard = initView(cardMap);
        play(coverageRelations, viewCard);

    }

    private static void play(List<Node> coverageRelations, List<Node> viewCard) {
        FullList<Node> playStorage = new FullList<>(7);
        Scanner scanner = new Scanner(System.in);
        for (; ; ) {
            show(viewCard, playStorage, coverageRelations);
            String line = scanner.nextLine();
            try {
                String[] commonds = line.split(",");
                Node commondNode = null;
                commondNode = moveViewCardToStorage(viewCard, playStorage, commonds, commondNode);
                if(!playStorage.isNotFull()){
                    System.out.println("Game Over");
                    break;
                }
                coverageCardToViewCard(coverageRelations, viewCard, commondNode);
                if(viewCard.size()==0){
                    System.out.println("恭喜挑战成功");
                }
            } catch (Exception e) {
                System.out.println("输入不合法");
            }
        }
    }

    private static Node moveViewCardToStorage(List<Node> viewCard, FullList<Node> playStorage, String[] commonds, Node commondNode) {
        for (Node node : viewCard) {
            if (node.level == Integer.parseInt(commonds[0]) && node.index == Integer.parseInt(commonds[1])) {
                playStorage.add(node);
                commondNode = node;
            }
        }
        if (commondNode == null) {
            System.out.println("输入的元素不存在");
        } else {
            viewCard.remove(commondNode);
        }
        List<Node> sameCategoryList = new ArrayList<>();
        for (Object obj : playStorage) {
            Node node = (Node) obj;
            if (node.category == commondNode.category) {
                sameCategoryList.add(node);
            }
        }
        if(sameCategoryList.size()==3){
            for (Node node:sameCategoryList){
                playStorage.remove(node);
            }
        }
        return commondNode;
    }

    private static void coverageCardToViewCard(List<Node> coverageRelations, List<Node> viewCard, Node commondNode) {
        List<Node> tempCoverageNodes = new ArrayList<>();
        for (Node node: coverageRelations){
            if(node.NodeList.size()>0){
                Node tempNode = null;

                for(Node childNode:node.NodeList){
                    if(childNode.level==commondNode.level && childNode.index== commondNode.index){
                        tempNode = childNode;
                    }
                }
                if(tempNode!=null){
                    node.NodeList.remove(tempNode);
                }
                if(node.NodeList.size()==0){
                    tempCoverageNodes.add(node);
                }
            }
        }
        if(tempCoverageNodes.size()>0){
            for (Node node:tempCoverageNodes){
                coverageRelations.remove(node);
                viewCard.add(node);
            }
        }
    }

    private static void show(List<Node> viewCards, FullList<Node> playStorage,List<Node> coverageRelations) {
        System.out.println("\n隐藏部分--------");
        for (Node node : coverageRelations) {
            System.out.print(node.level + "," + node.index + "[" + node.category + "]\t");
        }
        System.out.println("\n视野地图--------");
        for (Node node : viewCards) {
            System.out.print(node.level + "," + node.index + "[" + node.category + "]\t");
        }
        System.out.println("\n缓存区地图--------");
        for (Object obj : playStorage) {
            Node node = (Node) obj;
            System.out.print(node.level + "," + node.index + "[" + node.category + "]\t");
        }
        System.out.println();
    }

    private static List<Node> initView(List<FullList<Integer>> cardMap) {
        List<Node> viewCards = new ArrayList<>();
        FullList<Integer> viewLevel = cardMap.get(cardMap.size() - 1);
        for (int i = 0; i < viewLevel.size; i++) {
            Node node = new Node();
            node.level = cardMap.size() - 1;
            node.index = i;
            node.category = (int) viewLevel.get(i);
            viewCards.add(node);
        }
        return viewCards;
    }

    /**
     * 定义覆盖关系
     *
     * @param cardMap
     * @return
     */
    private static List<Node> generateConverageRelations(List<FullList<Integer>> cardMap) {
        List<Node> coverageRelations = new ArrayList<>();
        for (int i = 0; i < cardMap.size(); i++) {
            FullList<Integer> eachLavel = cardMap.get(i);
            for (int j = 0; j < eachLavel.size; j++) {
                if (i + 1 < cardMap.size()) {
                    Node currentNode = new Node();
                    currentNode.level = i;
                    currentNode.index = j;
                    currentNode.category = (int) eachLavel.get(j);
                    Node leftNode = new Node();
                    leftNode.level = i + 1;
                    leftNode.index = j;
                    leftNode.category = (int) cardMap.get(leftNode.level).get(j);
                    currentNode.NodeList.add(leftNode);
                    Node rightNode = new Node();
                    rightNode.level = i + 1;
                    rightNode.index = j + 1;
                    rightNode.category = (int) cardMap.get(rightNode.level).get(j);
                    currentNode.NodeList.add(rightNode);
                    coverageRelations.add(currentNode);
                }
            }
        }
        return coverageRelations;
    }

    /**
     * 将随机卡片填充到卡片地图中
     *
     * @param cardMap
     * @param randomCards
     */
    private static void mappingRandomCardsToCardMap(List<FullList<Integer>> cardMap, int[] randomCards) {
        int cardLevel = 0;
        for (int key : randomCards) {
            FullList<Integer> eachLevelCards = cardMap.get(cardLevel);
            if (eachLevelCards.isNotFull()) {
                eachLevelCards.add(key);
            } else {
                cardLevel++;
                eachLevelCards = cardMap.get(cardLevel);
                eachLevelCards.add(key);
            }
        }
        for (List<Integer> list : cardMap) {
            for (Integer card : list) {
                System.out.print(card + " ");
            }
            System.out.println();
        }
    }


    /**
     * 生成随机卡片
     *
     * @param dispelGroupNum
     * @param categorys
     * @return
     */
    private static int[] generatorRandomCards(int dispelGroupNum, int categorys) {
        Random random = new Random();
        int[] totalCardNum = new int[dispelGroupNum * 3];
        for (int i = 0; i < dispelGroupNum; i++) {
            int catgory = random.nextInt(categorys);
            for (int j = 0; j < 3; j++) {
                for (; ; ) {
                    int index = random.nextInt(totalCardNum.length);
                    if (totalCardNum[index] == 0) {
                        totalCardNum[index] = catgory;
                        break;
                    }
                }
            }
        }
        return totalCardNum;
    }

    /**
     * 定义初始化地图
     *
     * @return
     */
    private static List<FullList<Integer>> initCardMap() {
        FullList<Integer> level0 = new FullList<>(8);
        FullList<Integer> level1 = new FullList<>(9);
        FullList<Integer> level2 = new FullList<>(10);
        List<FullList<Integer>> cardMap = new ArrayList<>();
        cardMap.add(level0);
        cardMap.add(level1);
        cardMap.add(level2);
        return cardMap;
    }

    static class FullList<T> extends ArrayList {
        int size;

        public FullList(int size) {
            this.size = size;
        }

        public boolean isNotFull() {
            return this.size() != this.size;
        }
    }

    /**
     * 覆盖关系
     */
    public static class Node {
        int level;
        Integer index;
        int category;
        List<Node> NodeList = new ArrayList<>();
    }
}
